package com.cim.service.impl;

import com.cim.dao.AccountDao;
import com.cim.pojo.Account;
import com.cim.service.AccountService;
import com.cim.utils.CharUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;



@Service
public class AccountServiceImpl implements AccountService {
    @Autowired
    private AccountDao accountDao;

    public Account getAccount(String id) {
        return accountDao.getAccount(id);
    }

    public List<Account> findAccount(Account filter) {
        List<Account> list=accountDao.findAccount(filter);
        return list;
    }

    public int addAccounts(List<Account> accounts) {
        for(Account account:accounts){
            addAccount(account);
        }
        return 0;
    }


    public Account getAccountByUserName(String userName){
        if(userName==null){
            return null;
        }
        return accountDao.getAccountByUserName(userName);
    }

    public String addAccount(Account account) {
        String id= CharUtil.getUUID(6);
        account.setUserId(id);
        try{
            accountDao.addAccount(account);
            return id;
        }catch(Exception e){
            e.printStackTrace();
            return addAccount(account);
        }
    }

    public int deleteAccount(String id) {
        return accountDao.deleteAccount(id);
    }

    public int updateAccount(Account account) {
        return accountDao.updateAccount(account);
    }

    public Account login(String userId){
        return accountDao.getAccount(userId);
    }
}
